<template>
    <div>
        <el-card class="top-card" style="text-align: right">
            <el-button type="primary" @click="drugsDialogVisible = true">添加药品</el-button>
            <el-button type="primary" @click="handleOpenBatchProcess">批量设置</el-button>
            <el-button type="primary" @click="handleSubmitAudit">提交审核</el-button>
            <el-button type="primary" @click="handleWarehousing">直接入库</el-button>
        </el-card>

        <el-card class="top-card" style="margin-top: 10px">
            <div style="display: inline-block">
                单据号：{{batchesOfInventory.batchesOfInventoryNumber}}
                <span style="margin-left: 20px">制单人：web01</span>
            </div>
            <div style="display: inline-block;position: relative;left: 40%">
                供应商：
                <el-select :value="batchesOfInventory.supplierId" v-model="batchesOfInventory.supplierId">
                    <el-option label="请选择供应商" value=""></el-option>
                    <el-option
                            v-for="supplier in suppliers"
                            :key="supplier.sid"
                            :value="supplier.sid"
                            :label="supplier.supplierName"
                    ></el-option>
                </el-select>
                制单日期：
                <el-date-picker
                        v-model="batchesOfInventory.batchesOfInventoryDate"
                        type="date"
                        placeholder="选择日期"
                        value-format="yyyy-MM-dd"
                >
                </el-date-picker>
            </div>
        </el-card>

        <div style="text-align: right;margin-top: 20px">
            批发总金额：<span style="color: red">{{batchesOfInventoryTotalMoney}}</span>
            <span style="margin-left: 30px">处方总金额：<span style="color: red">{{batchesOfSaleTotalMoney}}</span></span>
            <el-button style="margin-left: 10px" @click="clearNumber" type="danger">清空数值</el-button>
        </div>

        <el-table :data="purchases" border style="margin-top: 10px" @selection-change="handleSelectionChange">
            <el-table-column type="selection"></el-table-column>
            <el-table-column label="药品名称" prop="medicinesName"></el-table-column>
            <el-table-column label="规格" prop="specifications"></el-table-column>
            <el-table-column label="数量" prop="purchaseNum">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.purchaseNum"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="单位" prop="purchaseUnit"></el-table-column>
            <el-table-column label="批发价" prop="purchaseTradePrice">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.purchaseTradePrice"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="处方价" prop="purchasePrescriptionPrice">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.purchasePrescriptionPrice"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="批发额" prop="purchaseTradeTotalAmount">
                <template slot-scope="scope">{{scope.row.purchaseTradePrice * scope.row.purchaseNum}}</template>
            </el-table-column>
            <el-table-column label="处方额" prop="purchasePrescriptionTotalAmount">
                <template slot-scope="scope">{{scope.row.purchasePrescriptionPrice * scope.row.purchaseNum}}</template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button @click="handleRemoveRow(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog
                title="药品选择"
                :visible.sync="drugsDialogVisible"
                width="50%">
            <drugs v-if="drugsDialogVisible" @cancel="drugsDialogVisible = false" @submit="handleGetDrugs"></drugs>
        </el-dialog>

        <el-dialog
                title="批量设置"
                :visible.sync="batchProcessDialogVisible"
                width="25%">
            <batch-process v-if="batchProcessDialogVisible" @submit="handleBathProcess"></batch-process>
        </el-dialog>
    </div>
</template>

<script>
    //引入采购单服务类
    import {BatchesOfInventoryService} from "../../../model/gs/BatchesOfInventoryService";
    //引入供应商服务类
    import {SupplierService} from "../../../model/gs/SupplierService";

    const Drugs = () => import('../../common/drugs/Drugs.vue')
    const BatchProcess = () => import('./batchProcess.vue')

    const batchesOfInventoryService = BatchesOfInventoryService.getInstance()
    const supplierService = SupplierService.getInstance()

    export default {
        data() {
            return {
                drugsDialogVisible: false,
                batchProcessDialogVisible: false,
                batchesOfInventory: {
                    batchesOfInventoryNumber: '',
                    companyId: 1,
                    supplierId: '',
                    batchesOfInventoryDate: '',
                    batchesOfInventoryTotalMoney: 0,
                    batchesOfSaleTotalMoney: 0
                },
                purchases: [],
                suppliers: [],
                checkeds: []
            }
        },
        methods: {
            //设置药品列表的方法
            handleGetDrugs(selections) {
                selections.forEach(selection => {
                    this.purchases.push({
                        index: this.purchases.length,
                        medicinesId: selection.medicinesId,
                        purchaseNum: 0,
                        purchaseUnit: selection.unit,
                        purchaseTradePrice: 0,
                        purchasePrescriptionPrice: 0,
                        purchaseTradeTotalAmount: 0,
                        purchasePrescriptionTotalAmount: 0,
                        medicinesName: selection.medicinesName,
                        specifications: selection.conversion + selection.unit,
                        inventoryStatus: 0
                    })
                })
                this.drugsDialogVisible = false
            },
            //查询最大采购单编号的方法
            handleGetCode() {
                batchesOfInventoryService.renderCode().then(({data}) => {
                    if (data.code === 0) {
                        this.batchesOfInventory.batchesOfInventoryNumber = data.maxCode
                    }
                })
            },
            //获取供应商列表
            handleGetSuppliers() {
                supplierService.list().then(({data}) => {
                    if (data.code === 0) {
                        this.suppliers = data.data
                    }
                })
            },
            //删除一行的方法
            handleRemoveRow(row) {
                for (let i = row.index + 1; i < this.purchases.length; i++) {
                    this.purchases[i].index -= 1
                }
                this.purchases.splice(row.index, 1)
            },
            clearNumber() {
                this.purchases.forEach(item => {
                    item.purchaseNum = 0
                    item.purchaseTradePrice = 0
                    item.purchasePrescriptionPrice = 0
                })
            },
            //打开批量设置的方法
            handleOpenBatchProcess() {
                //判断是否选了数据
                if (this.checkeds.length === 0) {
                    this.$message.warning({
                        message: '请选择药品',
                        duration: 1000
                    })
                    return
                }
                this.batchProcessDialogVisible = true
            },
            //批量设置的方法
            handleBathProcess(formData) {
                this.checkeds.forEach(item => {
                    item.purchaseNum = formData.num
                    item.purchaseTradePrice = formData.inventoryPrice
                    item.purchasePrescriptionPrice = formData.salePrice
                })
                this.batchProcessDialogVisible = false
            },
            //表格选中改变事件
            handleSelectionChange(selection) {
                this.checkeds = selection
            },
            //直接入库的方法
            handleWarehousing() {
                this.handleSetSubmitValue(3)
                //提交
                batchesOfInventoryService
                    .wareHousingBatchesOfInventory(this.batchesOfInventory, this.purchases)
                    .then(({data}) => {
                        if (data.code === 0) {
                            this.$message.success({
                                message: '入库成功',
                                duration: 1000
                            })
                            this.clearData()
                        } else {
                            this.$message.error({
                                message: data.message,
                                duration: 2000
                            })
                        }
                    })
            },
            handleSubmitAudit() {
                this.handleSetSubmitValue(1)
                //提交
                batchesOfInventoryService
                    .submitBatchesOfInventory(this.batchesOfInventory, this.purchases)
                    .then(({data}) => {
                        if (data.code === 0) {
                            this.$message.success({
                                message: '提交成功',
                                duration: 1000
                            })
                            this.clearData()
                        } else {
                            this.$message.error({
                                message: data.message,
                                duration: 2000
                            })
                        }
                    })
            },
            //设置提交数据
            handleSetSubmitValue(status) {
                //给每种药品设置批发总额和处方总额
                this.purchases.forEach(item => {
                    item.purchaseTradeTotalAmount = item.purchaseTradePrice * item.purchaseNum
                    item.purchasePrescriptionTotalAmount = item.purchasePrescriptionPrice * item.purchaseNum
                })
                //给batchesOfInventory对象设置采购总额
                this.batchesOfInventory.batchesOfInventoryTotalMoney = this.batchesOfInventoryTotalMoney
                //设置审批状态
                this.batchesOfInventory.batchesOfInventoryStatus = status
                this.batchesOfInventory.batchesOfInventoryVerifier = 0
            },
            //重置batchesOfInventory+purchases的方法
            clearData() {
                this.purchases = []
                this.batchesOfInventory = {
                    batchesOfInventoryNumber: '',
                    companyId: 1,
                    supplierId: '',
                    batchesOfInventoryDate: '',
                    batchesOfInventoryTotalMoney: 0,
                    batchesOfSaleTotalMoney: 0
                }
                this.handleGetCode()
            }
        },
        //计算属性
        computed: {
            batchesOfInventoryTotalMoney: {
                get() {
                    return this.purchases.length !== 0 ? eval(this.purchases
                        .map(item => item.purchaseTradePrice * item.purchaseNum)
                        .join('+')) : 0
                }
            },
            batchesOfSaleTotalMoney: {
                get() {
                    return this.purchases.length !== 0 ? eval(this.purchases
                        .map(item => item.purchasePrescriptionPrice * item.purchaseNum)
                        .join('+')) : 0
                }
            }
        },
        created() {
            this.handleGetCode()
            this.handleGetSuppliers()
        },
        components: {
            Drugs,
            BatchProcess
        }
    }
</script>

<style scoped>

</style>
